package com.aiti.lulian.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.LuLianAnnouncement;
import com.aiti.lulian.service.ILuLianAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author WongMz
 * @CreateDate 2024-08-19
 * @Description: 通知公告控制层
 */
@Api(tags = "通知公告控制层")
@RestController
@RequestMapping("/announcement")
public class LuLianAnnouncementController extends WebController {

    protected final Logger logger = LoggerFactory.getLogger(LuLianAnnouncementController.class);

    @Autowired
    private ILuLianAnnouncementService announcementService;

    /**
     * @Author WongMz
     * @CreateDate 2024-08-19
     * @Description: 发布通知公告
     */
    @ApiOperation("发布通知公告")
    @PostMapping
    public Message save(@Validated @RequestBody LuLianAnnouncement announcement) {
        try {
            if (announcement == null || BeanUtil.isEmpty(announcement)) {
                logger.error("【save】方法新增异常：===》入参异常");
                return Fail("入参异常！");
            }
            String userId = findUserId();
            if (StrUtil.isNotBlank(userId)) {
                announcement.setCreator(userId);
            } else {
                System.out.println("------------------未登录------------------");
                return Fail("请先注册/登录！");
            }
            if (announcementService.save(announcement)) {
                return Success("发布成功！");
            } else {
                return Fail("发布失败！");
            }
        } catch (Exception e) {
            logger.error("【save】方法新增异常：===》" + e);
            return Fail(e.getMessage());
        }
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-08-19
     * @Description: 查询通知公告列表
     */
    @ApiOperation("查询通知公告列表")
    @GetMapping("/list")
    public Message queryList(Integer pageNum, Integer pageSize, LuLianAnnouncement announcement) {
        try {
            if (pageNum == null || pageSize == null || announcement == null || BeanUtil.isEmpty(announcement)) {
                logger.error("【queryList】方法查询异常：===》入参异常");
                return Fail("入参异常！");
            }
            return Success(announcementService.queryList(pageNum, pageSize, announcement));
        } catch (Exception e) {
            logger.error("【queryList】方法查询异常：===》" + e);
            return Fail(e.getMessage());
        }
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-08-19
     * @Description: 根据baseId查询通知公告详情
     */
    @ApiOperation("根据baseId查询通知公告详情")
    @GetMapping("/{baseId}")
    public Message queryDetailsByBaseId(@PathVariable("baseId") String baseId) {
        try {
            if (StrUtil.isBlank(baseId)) {
                logger.error("【queryDetailsByBaseId】方法查询异常：===》入参异常");
                return Fail("入参异常！");
            }
            return Success(announcementService.getById(baseId));
        } catch (Exception e) {
            logger.error("【queryDetailsByBaseId】方法查询异常：===》" + e);
            return Fail(e.getMessage());
        }
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-08-19
     * @Description: 根据baseId修改通知公告信息
     */
    @ApiOperation("根据baseId修改通知公告信息")
    @PutMapping
    public Message updateByBaseId(@Validated @RequestBody LuLianAnnouncement announcement) {
        try {
            if (announcement == null || BeanUtil.isEmpty(announcement)) {
                logger.error("【updateByBaseId】方法更新异常：===》入参异常");
                return Fail("入参异常！");
            }
            String userId = findUserId();
            if (StrUtil.isNotBlank(userId)) {
                announcement.setUpdateBy(userId);
            } else {
                System.out.println("------------------未登录------------------");
                return Fail("请先注册/登录！");
            }
            String resSucMsg = StrUtil.equals("1", announcement.getIsDel()) ? "删除成功！" : "修改成功！";
            String resFailMsg = StrUtil.equals("1", announcement.getIsDel()) ? "删除失败！" : "修改失败！";
            if (announcementService.updateById(announcement)) {
                return Success(resSucMsg);
            } else {
                return Fail(resFailMsg);
            }
        } catch (Exception e) {
            logger.error("【updateByBaseId】方法更新异常：===》" + e);
            return Fail(e.getMessage());
        }
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-08-19
     * @Description: 根据baseId删除通知公告
     */
    @ApiOperation("根据baseId删除通知公告")
    @DeleteMapping("/{baseId}")
    public Message deleteByBaseId(@PathVariable("baseId") String baseId) {
        try {
            if (StrUtil.isBlank(baseId)) {
                logger.error("【deleteByBaseId】方法删除异常：===》入参异常");
                return Fail("入参异常！");
            }
            String userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                System.out.println("------------------未登录------------------");
                return Fail("请先注册/登录！");
            }
            if (announcementService.removeById(baseId)) {
                return Success("删除成功！");
            } else {
                return Fail("删除失败！");
            }
        } catch (Exception e) {
            logger.error("【deleteByBaseId】方法删除异常：===》" + e);
            return Fail(e.getMessage());
        }
    }
}